<?php
if (!defined('ROOT_PATH')) die('No direct access.');
function getArtikelnya($id){
	$db = new db();
	$query = "SELECT tanggal,judul,isi,tag,nama,idArtikel,`slug-judul`,type,idReview,idFB FROM `Artikel` INNER JOIN Pengguna ON idPublisher = idPengguna WHERE idPublisher = '$id' ORDER BY tanggal DESC";
	$ret = $db->query($query);
	return $ret;
}

function getTransBeli($id){
	$db = new db();
	$query = "SELECT * FROM Transaksi t
				INNER JOIN Alamat a ON t.idAlamat = a.idAlamat
				WHERE t.idPembeli = '$id' AND t.status < '3' ORDER BY t.tanggal ASC";
	//echo $query;die();
	$ret = $db->query($query);
	return $ret;
}

function getTransJual($id){
	$db = new db();
	$query = "SELECT t.idTransaksi,t.`no-invoice`,t.tanggal,t.idPembeli,p.idPesanan,p.idBarang,p.kuantitas,p.biayakirim,p.status,a.kota,a.lengkap,a.nama_penerima,a.no_kontak,b.nama
				FROM Pesanan p
				INNER JOIN Transaksi t ON t.idTransaksi = p.idTransaksi
				INNER JOIN Alamat a ON t.idAlamat = a.idAlamat
				INNER JOIN Barang b ON p.idBarang = b.idBarang
				WHERE p.idPenjual = '$id' AND p.status < '3' ORDER BY t.tanggal ASC";
	//echo $query;die();
	$ret = $db->query($query);
	return $ret;
}

function getHistJual($id){
	$db = new db();
	$query = "SELECT t.idTransaksi,t.`no-invoice`,t.tanggal,t.idPembeli,p.idPesanan,p.idBarang,p.kuantitas,p.biayakirim,p.status,a.kota,a.lengkap,a.nama_penerima,a.no_kontak,b.nama
				FROM Pesanan p
				INNER JOIN Transaksi t ON t.idTransaksi = p.idTransaksi
				INNER JOIN Alamat a ON t.idAlamat = a.idAlamat
				INNER JOIN Barang b ON p.idBarang = b.idBarang
				WHERE p.idPenjual = '$id' AND p.status = '3' ORDER BY t.tanggal ASC";
	//echo $query;die();
	$ret = $db->query($query);
	return $ret;
}

function getHistBeli($id){
	$db = new db();
	$query = "SELECT * FROM Transaksi t
				INNER JOIN Alamat a ON t.idAlamat = a.idAlamat
				WHERE t.idPembeli = '$id' AND t.status = '3' ORDER BY tanggal ASC";
	//echo $query;die();
	$ret = $db->query($query);
	return $ret;
}

function statTrans($stat){
	switch($stat){
		case 1 :
			$text = "Belum bayar";
			break;
		case 2:
			$text = "Sudah bayar";
			break;
	}
	return $text;
}

function statPesanan($stat){
	switch($stat){
		case 1 :
			$text = "Belum bayar";
			break;
		case 2:
			$text = "Sudah bayar";
			break;
		case 3:
			$text = "Sudah kirim";
			break;
		case 4:
			$text = "Sudah diterima";
			break;
	}
	return $text;
}

function editProfil($id,$nama,$kontak,$email,$desc){
	$db = new db();
	$query = "UPDATE Pengguna SET `nama`='$nama',`kontak`='$kontak',`email`='$email',`bio`='$desc' WHERE idPengguna=$id";
	$ret = 0;
	$db->exec($query);
	$ret = $db->rowCount();
	if ($ret==0)
		$_SESSION['message'] = "Gagal ubah profil";
	else
		$_SESSION['message'] = "Sukses ubah profil";
	return $ret;
}

function addAlamat($id,$nama,$kontak,$kota,$alamat){
	$db = new db();
	$query = "INSERT INTO Alamat (idPembeli, kota, lengkap, nama_penerima, no_kontak) VALUES ($id,'$kota', '$alamat', '$nama', '$kontak')";
	$ret = 0;
	$db->exec($query);
	$ret = $db->lastInsertId();
	return $ret;
}

function editAlamat($id,$nama,$kontak,$kota,$alamat){
	$db = new db();
	$query = "UPDATE Alamat SET `nama_penerima`='$nama',`no_kontak`='$kontak',`kota`='$kota',`lengkap`='$alamat' WHERE idAlamat=$id";
	$ret = 0;
	$db->exec($query);
	$ret = $db->rowCount();
	return $ret;
}
